package com.itheima.edu.info.managers.dao;

import com.itheima.edu.info.managers.domain.Student;

public class StudentDao {
    private static Student[] students = new Student[10];

    public boolean addStudent(Student student) {
        //创建一个学生数组，动态初始化
        //Student[] students=new Student[10];
        //设置他的索引不存在，遍历数组
        //2遍历数组取出每一个元素，判断是否是null
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            //如果为null，让index变量记录当前索引位置，并使用break结束循环遍历
            if (stu == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            //装满了
            return false;
        } else {
            //可以添加
            students[index] = student;
            return true;
        }

    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudent(String delId) {
        int index = getIndex(delId);

        students[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu != null && stu.getId().equals(id)) {
                index = i;
            }
        }
        return index;

    }

    public void updateStudent(Student stu, String updateId) {
        int index = getIndex(updateId);
        students[index] = stu;
    }
}
